<template>
    <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="搜索标题"
        style="width: 220px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
        <el-select
        v-model="listQuery.pkg"
        style="width: 150px"
        class="filter-item"
        filterable
        @change="handleFilter"
      >
      <el-option label="全部包" :value="''" />
      <el-option v-for="(v, k) in pkgs" :key="k" :label="v" :value="k" />
      </el-select>
      <el-select
        v-model="listQuery.status"
        style="width: 150px"
        class="filter-item"
        @change="handleFilter"
      >
        <el-option key label="全部状态" :value="''" />
        <el-option key="2" label="已停用" :value="2" />
        <el-option key="1" label="已启用" :value="1" />
      </el-select>
      <el-button
       plain size="small"
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <el-button
       plain size="small"
        v-waves
        class="filter-item btn"
        type="primary"
        icon="el-icon-refresh"
        @click="handleCancel"
      >重置</el-button>
      <el-button
       plain size="small"
        v-waves
        class="filter-item btn"
        type="success"
        icon="el-icon-plus"
        @click="handleAdd()"
      >新增落地页</el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="id" align="center" prop="id" width="60"/>
      <el-table-column label="标题" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="链接" align="center">
        <template slot-scope="{row}">
          <span>{{ row.url }}</span>
        </template>
      </el-table-column>
      <el-table-column label="包" align="center">
        <template slot-scope="{row}">
          <span> {{ pkgs[row.pkg] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" min-width="100" align="center">
        <template slot-scope="{row}">
          <el-image 
            style="width: 50px; height: 50px"
            :src="row.img_url" 
            :preview-src-list="[row.img_url]">
        </el-image>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="100" align="center">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.status"
            :active-value="'1'"
            :inactive-value="'2'"
            @change="changeStatus(row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <el-button type="primary" plain size="small" @click="handleUpdate(row)">编辑</el-button>
          <!-- <el-button type="danger" plain size="small" @click="handleDel(row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      class="pageShow"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog
      title="sad"
      :visible.sync="updated"
      :width="dialogWidth"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="90px"
        class="fullwidth"
        enctype="multipart/form-data"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="temp.title"
            placeholder="标题"
          />
        </el-form-item>
        <el-form-item label="链接" prop="url">
          <el-input v-model="temp.url" placeholder="链接" />
        </el-form-item>
        <el-form-item label="包" prop="pkg">
           <el-select
            v-model="temp.pkg"
            style="width: 100%"
            class="filter-item"
            filterable
          >
          <el-option label="全部包" :value="''" />
          <el-option v-for="(v, k) in pkgs" :key="k" :label="v" :value="k" />
          </el-select>
        </el-form-item>
        <el-form-item label="图片" >
            <el-upload
                name="img"
                class="avatar-uploader"
                action="http://kyy.jianyue668.com/api/upload/img"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>

        <el-form-item label="状态">
            <el-radio v-model="temp.status" label="1">开启</el-radio>
            <el-radio v-model="temp.status" label="0">关闭</el-radio>
        </el-form-item>
        <div style="text-align: right">
          <el-button plain size="small" @click="updated = false">取消</el-button>
          <el-button
           plain size="small"
            type="primary"
            :loading='addloading'
            @click="dialogStatus==='create'?handadd():handupdate()"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
    getLangingPage,
    addLangingPage,
    UpdateLangingPage,
    DelLangingPage
} from "@/api/admin/admin";
import {
    getPkgList
  } from "@/api/adv_admin/config";
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { Message, MessageBox } from "element-ui";
import {pkgs} from '@/utils/index'
export default {
    'name': 'LandingPage',
    components: { Pagination },
    directives: { waves },
    data() {
        return {
            imageUrl: '',
            tableKey: 0,
            listLoading: false,
            addloading: false,
            dialogWidth: '0px',
            fileList: [],
            list: [],
            total: 0,
            listQuery: {
                page: 1,
                limit: 20,
                pkg: "",
                title: "",
                status: "",
            },
            rules: {
                title: [
                    { required: true, message: "标题", trigger: "blur" }
                ],
                url: [
                    { required: true, message: "链接", trigger: "blur" }
                ],
                pkg: [
                    { required: true, message: "必选", trigger: "blur" }
                ]
            },
            updated: false,
            update: '',
            dialogStatus: '',
            temp: {
                id: '',
                title: '',
                url:'',
                img_url:'',
                status: '',
                pkg: ''
            },
            pkgs: []
        }
    },
    mounted(){
      this.getPkgs();
      this.getList()
      this.setDialogWidth()
    },
    methods: {
      getPkgs(){
        getPkgList().then((res)=>{
          this.pkgs = res.DATA.list
        })
      },
        setDialogWidth() {
            const val = document.body.clientWidth;
            const def = 600; // 默认宽度
            if (val < def) {
                this.dialogWidth = "100%";
            } else {
                this.dialogWidth = def + "px";
            }
        },
        getList(){
            this.listLoading = true
            getLangingPage(this.listQuery).then(res => {
                if (res.STATUS == 200){
                    this.list = res.DATA.list
                    this.total = res.DATA.total
                }
                this.listLoading = false
            })
        },
        handleFilter() {
            this.listQuery.page = 1;
            this.getList();
        },
        handleCancel() {
            this.listQuery.page = 1;
            this.listQuery.title = "";
            this.listQuery.status = "";
            this.listQuery.pkg = "";
            this.getList();
        },
        handleUpdate(row){
            this.updated = true
            this.temp.id = row.id
            this.temp.title = row.title
            this.temp.url = row.url
            this.temp.img_url = ''
            this.imageUrl = ''
            this.temp.pkg = row.pkg
            this.temp.status = row.status
            this.dialogStatus = 'update'
        },
        handleAdd(){
            this.updated = true
            this.temp.id = ''
            this.temp.title = ''
            this.temp.url = ''
            this.temp.img_url = ''
            this.imageUrl = ''
            this.temp.pkg = ""
            this.temp.status = ''
            this.dialogStatus = 'create'
        },
        handadd(){
            this.addloading = true
            // console.log(this.temp)
            addLangingPage(this.temp).then(res => {
                if (res.STATUS == 200){
                    Message.success('添加成功')
                    this.updated = false
                    this.getList()
                }else{
                    
                    Message({
                        message: res.DEC,
                        type: "error",
                        duration: 2000,
                    });
                    // Message.error()
                }
                this.addloading = false
            })
        },
        beforeAvatarUpload(file) {
        },
        handleAvatarSuccess(res, file){
            this.temp.img_url = res.DATA
            // this.imageUrl = 'http://apichannel.jianyue668.com' + res.DATA
            this.imageUrl = 'http://kyy.jianyue668.com' + res.DATA
            // console.log(res,file)
        },
        handupdate(){
            this.addloading = true
            UpdateLangingPage(this.temp).then(res => {
                if (res.STATUS == 200){
                    Message.success('修改成功')
                    this.updated = false
                    this.getList()
                }else{
                    
                    Message({
                        message: res.DEC,
                        type: "error",
                        duration: 2000,
                    });
                    // Message.error()
                }
                this.addloading = false
                this.imageUrl = ''
            })

        },
        handleDel(item){
            MessageBox.confirm("确定删除吗", "确认", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                DelLangingPage({ id: item.id }).then((r) => {
                    if (r.STATUS == 200) {
                        Message({
                            message: "删除成功",
                            type: "success",
                            duration: 2000,
                        });
                        this.getList();
                    }
                });
            }).catch(() => {});
            
        },
        changeStatus(item){
          UpdateLangingPage({status: item.status,id: item.id}).then(res => {
                if (res.STATUS == 200){
                    Message.success('修改成功')
                    this.updated = false
                    this.getList()
                }else{
                    
                    Message({
                        message: res.DEC,
                        type: "error",
                        duration: 2000,
                    });
                }
            })
        }
    }
}
</script>
<style>
.show-pwd {
  position: absolute;
  right: 10px;
  top: 0;
  font-size: 16px;
  color: #ccc;
  cursor: pointer;
  user-select: none;
}
.push {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  margin-left: 5px;
}
.pageHidden {
  display: none;
}
.btn {
  margin-left: 0;
}
.fullwidth {
  width: 450px;
  margin-left: 50px;
}
@media (max-width: 550px) {
  .pageShow {
    display: none;
  }
  .pageHidden {
    display: block;
  }
  .fullwidth {
    width: 100%;
    margin-left: 0;
  }
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 90px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 90px;
    height: 178px;
    display: block;
  }
  
</style>